package com.elaine.factory.alapi;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.elaine.constant.AlapiConstant;
import com.elaine.model.alapi.AlapiResponse;
import com.elaine.model.alapi.QinghuaData;
import com.elaine.utils.HttpUtils;
import org.springframework.stereotype.Component;

/**
 * @author wrb
 * @date 2020/3/31 15:38
 */
@Component
public class QinghuaAlapiFactory extends AlapiFactory {

	/**
	 * type 支持 json 和 text
	 */
	private static final String URL = "https://v1.alapi.cn/api/qinghua?type=json";

	@Override
	protected void registerFactory() {
		doRegister(AlapiConstant.QINGHUA, this);
	}

	@Override
	public String getReply() {
		String jsonStr = HttpUtils.requestGet(URL, null, null);
		AlapiResponse<QinghuaData> alapiResponse = JSON.parseObject(jsonStr, new TypeReference<AlapiResponse<QinghuaData>>() {});
		if (alapiResponse != null && alapiResponse.isSuccess()) {
			return alapiResponse.getData().getContent();
		}else {
			return "土味情话请求失败咯，ararar...";
		}
	}
}
